﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class ConsultingType : PowerBase
    {
        CJJCH.BLL.T_NET_MenuThird bll_Third = new BLL.T_NET_MenuThird();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSearch";

            if (!IsPostBack)
            {
                InitRequest();
                Repeater_DataItem();
            }

        }
        private void InitRequest()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {
                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = new CJJCH.BLL.T_NET_MenuSecond().GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {
                    if (model.HaveChild == true)
                    {
                        ConsultingTop1.TypeID = 1;
                    }
                    else
                    {
                        ConsultingTop1.TypeID = 0;
                    }
                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                    ConsultingTop1.SecondId = Convert.ToInt32(lblSecondId.Text);
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    ConsultingTop1.SecondName = LTP.Common.StringClass.EncodeString(lblSecondName.Text);
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }

            }

        }
        protected void Repeater_DataItem()
        {
            IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
            string strWhere = "";

            if (lblKeyWord.Text.Trim() != "")
            {
                strWhere = DelSQLStr(lblKeyWord.Text.Trim());
            }
            list = bll_Third.GetThird(Convert.ToInt32(lblSecondId.Text), strWhere);
            this.rptType.DataSource = list;
            this.rptType.DataBind();
            if (list.Count == 0)
            {
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            }
            else
            {
                litMessage.Text = "";
            }
        }
        protected void rptType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((TextBox)e.Item.FindControl("txtTypeName")).Text = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).MenuName;
                ((TextBox)e.Item.FindControl("txtOrder")).Text = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).OrderNum.ToString();
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定删除该类别吗？')){return true;}else{return false;}");
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).ID.ToString();
            }
        }
        protected string DelSQLStr(string str)
        {
            if (str == null || str == "")
                return "";
            str = str.Replace("'", "");
            str = str.Replace("--", "");
            str = str.Replace("<", "");
            str = str.Replace(">", "");
            str = str.Replace("%", "");
            str = str.Replace("=", "");
            str = str.Replace("\"", "");
            str = str.Replace(",", "");
            return str;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblKeyWord.Text = LTP.Common.StringClass.EncodeStr(txtKeyword.Text);
            Repeater_DataItem();
        }
        protected void lbtnAdd_Click(object sender, EventArgs e)
        {
            string error = CheckInput();
            if (error == "")
            {
                CJJCH.Model.T_NET_MenuThird model = new Model.T_NET_MenuThird();
                model.MenuName = LTP.Common.StringClass.EncodeString(txtType.Text.Trim());
                model.OrderNum = Convert.ToInt32(txtOrderNum.Text.Trim());
                model.SecondID = Convert.ToInt32(lblSecondId.Text.Trim());
                error = bll_Third.AddThird(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("添加成功", "ConsultingType.aspx?keyword="
                        + LTP.Common.StringClass.EncodeUrl(lblKeyWord.Text.Trim())
                        + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text)
                        + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text)
                        + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.MessageBox.Show(this, error);
            }
        }
        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            LinkButton linb = (LinkButton)sender;
            string res = "";
            res = bll_Third.DelStandardsThird(Convert.ToInt32(linb.CommandArgument));
            if (string.IsNullOrEmpty(res.Trim()))
            {
                Response.Redirect("ConsultingType.aspx?keyword="
                    + LTP.Common.StringClass.EncodeUrl(lblKeyWord.Text.Trim())
                    + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text)
                    + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text)
                    + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text));
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(res);
            }
        }
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            StringBuilder error = new StringBuilder();
            LinkButton linb = (LinkButton)sender;
            int Index = ((RepeaterItem)(linb.NamingContainer)).ItemIndex;
            TextBox txtTypeName = (TextBox)rptType.Items[Index].FindControl("txtTypeName");
            TextBox txtOrder = (TextBox)rptType.Items[Index].FindControl("txtOrder");
            if (txtTypeName.Text.Trim() == "")
            {
                error.Append("请输入类别名称！！\\n");
            }
            if (txtOrder.Text.Trim() == "")
            {
                error.Append("请输入排序号！！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrder.Text.Trim()))
            {
                error.Append("排序号必须是数字！！\\n");
            }

            if (!string.IsNullOrEmpty(error.ToString()))
            {
                LTP.Common.MessageBox.Show(this, error.ToString());
                return;
            }
            CJJCH.Model.T_NET_MenuThird model = new Model.T_NET_MenuThird();
            model.MenuName = LTP.Common.StringClass.EncodeString(txtTypeName.Text.Trim());
            model.OrderNum = Convert.ToInt32(txtOrder.Text.Trim());
            model.SecondID = Convert.ToInt32(lblSecondId.Text.Trim());
            model.ID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            string res = bll_Third.UpdateThird(model);
            LTP.Common.MessageBox.ShowAndRedirect(this, res, "ConsultingType.aspx?keyword="
                     + LTP.Common.StringClass.EncodeUrl(lblKeyWord.Text.Trim())
                     + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text)
                     + "&firstid=" + LTP.Common.StringClass.EncodeUrl(lblFirstID.Text)
                     + "&secondname=" + LTP.Common.StringClass.EncodeUrl(lblSecondName.Text));

        }
        protected string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtType.Text.Trim() == "")
            {
                error.Append("请输入类别名称！！\\n");
            }
            if (txtOrderNum.Text.Trim() == "")
            {
                error.Append("请输入排序号！！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrderNum.Text.Trim()))
            {
                error.Append("排序号必须是数字！！\\n");
            }
            return error.ToString();
        }

    }
}